package mapstruct.convert;

import mapstruct.dto.BoyRes;
import mapstruct.dto.GirlRes;
import mapstruct.po.BoyPo;
import mapstruct.po.GirlPo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * The class/interface 可以和spring融合
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
@Mapper
public interface ManConvert {
    ManConvert MEN_CONVERT = Mappers.getMapper(ManConvert.class);

    @Mappings(
            // 如果不排除，会报错
            @Mapping(target = "girlFriends", ignore = true)
    )
    BoyRes po2Res(BoyPo manPo);


    @Mapping(target = "sex", expression = "java(info(girlPo.getSex()))")
    GirlRes po2Res(GirlPo girlPo);

    default String info(Integer sex) {
        if (sex.equals(0))
            return "男";
        return "女";
    }

    // list到list，不需要添加注解，会沿用上面的类
    List<BoyRes> list2R(List<BoyPo> boyPos);

    List<GirlRes> list2Ra(List<GirlPo> boyPos);

    // 格式化
    @Mappings(
            {
                    @Mapping(target = "salary", source = "salary", numberFormat = "#.00", defaultValue = "0.00"),
                    @Mapping(target = "girlFriends", ignore = true)
            }
    )
    BoyPo toF(BoyRes boyPo);


    // 反转
    // @InheritInverseConfiguration(name = "toF")
}
